package views.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import models.util.BalloonThread;

import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.PropertySetter;
import org.jdesktop.core.animation.timing.TimingSource;
import org.jdesktop.core.animation.timing.TimingTarget;
import org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource;

public class SlotPanel extends JPanel/* implements TimingTarget */{

	private Random random = new Random();
	private final int START_Y = 450;
	private final int X = 10;
	private int balloonImageIndex;
	private static TimingSource ts = new SwingTimerTimingSource();
	private static boolean timingSourceRunning ;
	private Animator animator;
	private BalloonThread balloon;
	private JLabel balloonLabel;
	private Point start = new Point(X, START_Y);
	private Point end = new Point(X, 0);
	private static Image[] balloonImages;

	public SlotPanel() {
		balloonImages = new Image[5];
		String path = "resources/pallonciniMatematici/";
		String[] balloonImageNames = new String[] { "whiteBln.png",
				"redBln.png", "blueBln.png", "greenBln.png", "orangeBln.png" };
		try {
			for (int i = 0; i < balloonImages.length; i++) {
				balloonImages[i] = ImageIO.read(new File(path
						+ balloonImageNames[i]));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		setOpaque(false);
		Dimension dim = new Dimension(160, 680);
		this.setPreferredSize(dim);
		balloonLabel = new JLabel(new ImageIcon());
		Dimension bdim = new Dimension(110, 220);
		balloonLabel.setPreferredSize(bdim);
		add(balloonLabel);
		balloonLabel.setVerticalTextPosition(SwingConstants.TOP);
		balloonLabel.setHorizontalTextPosition(SwingConstants.CENTER);
		balloonLabel.setForeground(Color.WHITE);
		balloonLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
	}

	public void setBalloon(BalloonThread b) {
		balloonLabel.setText(b.toString());
		int duration = b.getDuration();
		balloon = b;
		if (!timingSourceRunning) {
			ts.init();
			timingSourceRunning = true;
		}
		Animator.setDefaultTimingSource(ts);
		animator = null;
		TimingTarget ps = PropertySetter.getTarget(this, "balloonLabel", start,
				end);
		animator = new Animator.Builder()
				.setDuration(duration, TimeUnit.SECONDS).addTarget(ps).build();
		animator.start();
		balloonImageIndex = random.nextInt(balloonImages.length);
		balloonLabel.setIcon(new ImageIcon(balloonImages[balloonImageIndex]));
	}

	public void removeBalloon() {
		if (animator != null && animator.isRunning())
			animator.stop();
		balloonLabel.setIcon(null);
		balloonLabel.setText("");
	}

	public JLabel getBalloonLabel() {
		return balloonLabel;
	}

	public void setBalloonLabel(Point newEnd) {
		balloonLabel.setLocation(newEnd);
	}

	public void close() {
		if (animator != null && animator.isRunning())
			animator.stop();
		if( timingSourceRunning){
			ts.dispose();
			timingSourceRunning = false;
		}
	}

}
